package mh.map;

import java.util.ArrayList;

import mh.OtherEntity;

/**
 * The layer that contains each of the entity of others player in this specific map.
 * 
 * @author Gianmarco Laggia
 * 
 */
public class OthersLayer extends Layer {

	/**
	 * The list of the entities in this layer
	 */
	private ArrayList<OtherEntity>	others; // TODO

	/**
	 * 
	 * @param x
	 *            The initial x position of the player sprite when created the map
	 * @param y
	 *            The initial y position of the player sprite when created the map
	 */
	public OthersLayer(double x, double y) {
		super(x, y, 0, 0);
		this.others = new ArrayList<OtherEntity>();
		for (int i = 0; i < 36; i++) {
			for (int j = 0; j < 28; j++) {
				if ((i == 8) && (j == 6)) {
					this.others.add(new OtherEntity("sprites/charset.png", 8, 6, this.actualx, this.actualy));
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mh.Layer#draw(double)
	 */
	@Override
	public void draw(long delta) {

		for (int i = 0; i < this.others.size(); i++) {
			OtherEntity other = this.others.get(i);
			other.setPlayerCoord(this.actualx, this.actualy);
			other.draw(delta);
		}

		this.lastFrameChange += delta;
		if (this.lastFrameChange > this.frameDuration) {
			this.lastFrameChange = 0;
			if (this.actualx < this.finalx) {
				this.actualx += 0.5;
			} else if (this.actualx > this.finalx) {
				this.actualx -= 0.5;
			} else if (this.actualy < this.finaly) {
				this.actualy += 0.5;
			} else if (this.actualy > this.finaly) {
				this.actualy -= 0.5;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mh.Layer#isAvaible(int, int)
	 */
	@Override
	public boolean isAvaible(int x, int y) {
		for (int i = 0; i < this.others.size(); i++) {
			OtherEntity other = this.others.get(i);
			if ((other.getX() == x) && (other.getY() == y)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Returns a list of all the Entities that are in this map.
	 * 
	 * @return A list of others.
	 */
	public ArrayList<OtherEntity> getOthers() {
		return this.others;
	}
}
